package es.udc.emarketapp.http.controller.actions.products;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import es.udc.emarketapp.emarket.model.countryFacade.CountryFacade;
import es.udc.emarketapp.emarket.model.country.vo.CountryVO;
import es.udc.emarketapp.http.view.actionForms.LocaleForm;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
import java.util.Locale;



public class EditLocaleAction extends DefaultAction {

    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";

    @Override
    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        LocaleForm localeForm = (LocaleForm) form;
        Locale locale = getLocale(request);

        localeForm.setLanguage(locale.getLanguage());
        localeForm.setCountry(locale.getCountry());


        //Aqui se hace la consulta para llenar el combo de paises
        try{
            CountryFacade countryFacade = new CountryFacade();

            List<CountryVO> countryVOs = countryFacade.findAllCountries(locale.getLanguage());
        
            request.setAttribute("countries", countryVOs);

            return mapping.findForward(SUCCESS);
    
        }catch(Exception e){
            return mapping.findForward(FAILURE);
        }
    }
}
